<template>
  <n-popover trigger="hover" placement="bottom-end" raw :show-arrow="false">
    <template #trigger>
      <n-button quaternary>
        <template #icon>
          <n-icon><Icons name="color-palette-outline" /></n-icon>
        </template>
        主题
      </n-button>
    </template>
    
    <div class="theme-selector-panel">
      <div class="theme-grid">
        <div 
          v-for="theme in presetThemes" 
          :key="theme.name"
          class="theme-option"
          :class="{ 'theme-option--active': currentTheme === theme.name }"
          @click="setTheme(theme.name)"
        >
          <div 
            class="theme-color-dot" 
            :style="{ 
              backgroundColor: theme.isDark ? '#2a2a2a' : theme.primaryColor 
            }"
          ></div>
          <span class="theme-label">{{ theme.label }}</span>
        </div>
        
        <div 
          v-if="customTheme"
          class="theme-option theme-option--custom-theme"
          :class="{ 'theme-option--active': currentTheme === 'custom' }"
          @click="setTheme('custom')"
        >
          <div class="theme-option-content">
            <div 
              class="theme-color-dot" 
              :style="{ 
                backgroundColor: customTheme.isDark ? '#2a2a2a' : customTheme.primaryColor 
              }"
            ></div>
            <span class="theme-label">{{ customTheme.label }}</span>
          </div>
          <div 
            class="theme-delete-btn"
            @click.stop="handleDeleteCustomTheme"
            title="删除自定义主题"
          >
            <n-icon size="14"><Icons name="trash-outline" /></n-icon>
          </div>
        </div>
      </div>
      
      <div class="theme-divider"></div>
      
      <div 
        class="theme-option theme-option--custom"
        @click="handleCustomTheme"
      >
        <n-icon class="custom-icon"><Icons name="color-palette-outline" /></n-icon>
        <span class="theme-label">自定义主题</span>
      </div>
    </div>
  </n-popover>

  <!-- 自定义主题模态框 -->
  <n-modal 
    v-model:show="showCustomModal" 
    preset="card" 
    style="width: 900px; max-height: 85vh;" 
    title="自定义主题"
    :mask-closable="false"
  >
    <div class="theme-form-container">
      <div class="form-section">
        <n-form :model="customThemeForm" label-placement="left" label-width="90px">
          <n-form-item label="主题名称">
            <n-input v-model:value="customThemeForm.label" placeholder="请输入主题名称" />
          </n-form-item>
          
          <n-form-item label="深色模式">
            <n-switch v-model:value="customThemeForm.isDark" />
          </n-form-item>

          <n-divider>主题色彩</n-divider>
          
          <div class="color-grid">
            <n-form-item label="主色调">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.primaryColor" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.primaryColor" 
                  placeholder="#18a058"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
            
            <n-form-item label="成功色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.successColor" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.successColor" 
                  placeholder="#52c41a"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
          </div>
          
          <div class="color-grid">
            <n-form-item label="警告色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.warningColor" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.warningColor" 
                  placeholder="#fa8c16"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
            
            <n-form-item label="错误色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.errorColor" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.errorColor" 
                  placeholder="#f5222d"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
          </div>
          
          <n-form-item label="信息色">
            <div class="color-picker-container">
              <input 
                type="color" 
                v-model="customThemeForm.infoColor" 
                class="color-picker"
              />
              <n-input 
                v-model:value="customThemeForm.infoColor" 
                placeholder="#1890ff"
                style="margin-left: 8px;"
              />
            </div>
          </n-form-item>

          <n-divider>背景与文字</n-divider>
          
          <div class="color-grid">
            <n-form-item label="主背景色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.backgroundColor" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.backgroundColor" 
                  :placeholder="customThemeForm.isDark ? '#1a1a1a' : '#ffffff'"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
            
            <n-form-item label="次背景色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.backgroundColorSecondary" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.backgroundColorSecondary" 
                  :placeholder="customThemeForm.isDark ? '#2a2a2a' : '#f5f5f5'"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
          </div>
          
          <div class="color-grid">
            <n-form-item label="主文字色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.textColor" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.textColor" 
                  :placeholder="customThemeForm.isDark ? '#ffffffe6' : '#333333'"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
            
            <n-form-item label="次文字色">
              <div class="color-picker-container">
                <input 
                  type="color" 
                  v-model="customThemeForm.textColorSecondary" 
                  class="color-picker"
                />
                <n-input 
                  v-model:value="customThemeForm.textColorSecondary" 
                  :placeholder="customThemeForm.isDark ? '#ffffff9a' : '#666666'"
                  style="margin-left: 8px;"
                />
              </div>
            </n-form-item>
          </div>
          
          <n-form-item label="边框色">
            <div class="color-picker-container">
              <input 
                type="color" 
                v-model="customThemeForm.borderColor" 
                class="color-picker"
              />
              <n-input 
                v-model:value="customThemeForm.borderColor" 
                :placeholder="customThemeForm.isDark ? '#3a3a3a' : '#f0f0f0'"
                style="margin-left: 8px;"
              />
            </div>
          </n-form-item>
        </n-form>
      </div>
      
      <div class="preview-section">
        <!-- 预览区域 -->
        <n-divider>预览效果</n-divider>
        <div 
          class="theme-preview"
          :style="{
            backgroundColor: customThemeForm.backgroundColor || (customThemeForm.isDark ? '#1a1a1a' : '#ffffff'),
            color: customThemeForm.textColor || (customThemeForm.isDark ? '#ffffffe6' : '#333333'),
            borderColor: customThemeForm.borderColor || (customThemeForm.isDark ? '#3a3a3a' : '#f0f0f0')
          }"
        >
          <div 
            class="preview-content"
            :style="{
              backgroundColor: customThemeForm.backgroundColorSecondary || (customThemeForm.isDark ? '#2a2a2a' : '#f5f5f5'),
              padding: '12px',
              borderRadius: '6px',
              marginBottom: '12px',
              border: '1px solid ' + (customThemeForm.borderColor || (customThemeForm.isDark ? '#3a3a3a' : '#f0f0f0'))
            }"
          >
            <div :style="{ color: customThemeForm.textColorSecondary || (customThemeForm.isDark ? '#ffffff9a' : '#666666') }">
              次级背景区域文字
            </div>
          </div>
          
          <n-space>
            <n-button type="primary" size="small" :style="{ backgroundColor: customThemeForm.primaryColor }">
              主按钮
            </n-button>
            <n-button type="success" size="small" :style="{ backgroundColor: customThemeForm.successColor }">
              成功
            </n-button>
            <n-button type="warning" size="small" :style="{ backgroundColor: customThemeForm.warningColor }">
              警告
            </n-button>
          </n-space>
          
          <n-space style="margin-top: 8px;">
            <n-button type="error" size="small" :style="{ backgroundColor: customThemeForm.errorColor }">
              错误
            </n-button>
            <n-button type="info" size="small" :style="{ backgroundColor: customThemeForm.infoColor }">
              信息
            </n-button>
          </n-space>
          
          <div style="margin-top: 12px; font-size: 14px;">
            <div>主文字颜色示例</div>
            <div :style="{ color: customThemeForm.textColorSecondary || (customThemeForm.isDark ? '#ffffff9a' : '#666666') }">
              次文字颜色示例
            </div>
          </div>
        </div>
      </div>
    </div>


    <template #footer>
      <n-space justify="end">
        <n-button @click="showCustomModal = false">取消</n-button>
        <n-button type="primary" @click="handleSaveCustomTheme">保存主题</n-button>
      </n-space>
    </template>
  </n-modal>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useTheme, presetThemes, type ThemeConfig } from '@/composables/useTheme'
import { 
  NPopover,
  NButton, 
  NIcon, 
  NModal, 
  NForm, 
  NFormItem, 
  NInput, 
  NSwitch, 
  NDivider,
  NSpace
} from 'naive-ui'
import Icons from './Icons.vue'

const { currentTheme, setTheme, setCustomTheme, customTheme, deleteCustomTheme } = useTheme()

const showCustomModal = ref(false)

const customThemeForm = ref<ThemeConfig>({
  name: 'custom',
  label: '自定义主题',
  primaryColor: '#18a058',
  successColor: '#52c41a',
  warningColor: '#fa8c16',
  errorColor: '#f5222d',
  infoColor: '#1890ff',
  backgroundColor: '#ffffff',
  backgroundColorSecondary: '#f5f5f5',
  textColor: '#333333',
  textColorSecondary: '#666666',
  borderColor: '#f0f0f0',
  isDark: false
})

// 不再需要themeOptions计算属性

// 更新默认颜色值（当深色模式切换时）
const updateDefaultColors = () => {
  if (customThemeForm.value.isDark) {
    // 深色模式默认值
    if (!customThemeForm.value.backgroundColor) customThemeForm.value.backgroundColor = '#1a1a1a'
    if (!customThemeForm.value.backgroundColorSecondary) customThemeForm.value.backgroundColorSecondary = '#2a2a2a'
    if (!customThemeForm.value.textColor) customThemeForm.value.textColor = '#ffffffe6'
    if (!customThemeForm.value.textColorSecondary) customThemeForm.value.textColorSecondary = '#ffffff9a'
    if (!customThemeForm.value.borderColor) customThemeForm.value.borderColor = '#3a3a3a'
  } else {
    // 浅色模式默认值
    if (!customThemeForm.value.backgroundColor) customThemeForm.value.backgroundColor = '#ffffff'
    if (!customThemeForm.value.backgroundColorSecondary) customThemeForm.value.backgroundColorSecondary = '#f5f5f5'
    if (!customThemeForm.value.textColor) customThemeForm.value.textColor = '#333333'
    if (!customThemeForm.value.textColorSecondary) customThemeForm.value.textColorSecondary = '#666666'
    if (!customThemeForm.value.borderColor) customThemeForm.value.borderColor = '#f0f0f0'
  }
}

// 处理自定义主题点击
const handleCustomTheme = () => {
  // 如果已有自定义主题，用它初始化表单
  if (customTheme.value) {
    customThemeForm.value = { ...customTheme.value }
  } else {
    // 确保有默认值
    updateDefaultColors()
  }
  showCustomModal.value = true
}

const handleSaveCustomTheme = () => {
  if (!customThemeForm.value.label.trim()) {
    customThemeForm.value.label = '自定义主题'
  }
  
  setCustomTheme(customThemeForm.value)
  showCustomModal.value = false
}

// 删除自定义主题
const handleDeleteCustomTheme = () => {
  // 如果当前正在使用自定义主题，切换到默认主题
  if (currentTheme.value === 'custom') {
    setTheme('default')
  }
  // 删除自定义主题
  deleteCustomTheme()
}

// Vue的h函数需要导入
import { h, watch } from 'vue'

// 监听深色模式切换，自动更新占位符和默认值
watch(() => customThemeForm.value.isDark, () => {
  updateDefaultColors()
})
</script>

<style scoped>
.color-picker-container {
  display: flex;
  align-items: center;
  width: 100%;
}

.color-picker {
  width: 40px;
  height: 32px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  outline: none;
}

.color-picker::-webkit-color-swatch-wrapper {
  padding: 0;
}

.color-picker::-webkit-color-swatch {
  border: 1px solid #d9d9d9;
  border-radius: 4px;
}

.theme-form-container {
  display: flex !important;
  gap: 24px !important;
  max-height: 65vh !important;
  overflow-y: auto !important;
}

.form-section {
  flex: 1 !important;
  min-width: 400px !important;
  padding-right: 12px !important;
}

.preview-section {
  flex: 0 0 320px !important;
  position: sticky !important;
  top: 0 !important;
  padding-left: 12px !important;
}

.color-grid {
  display: grid !important;
  grid-template-columns: 1fr 1fr !important;
  gap: 12px !important;
  margin-bottom: 12px !important;
}

.theme-preview {
  padding: 16px;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  background-color: #fafafa;
  max-height: 400px;
  overflow-y: auto;
}

.preview-content {
  margin-bottom: 12px;
}

/* 主题选择面板样式 */
.theme-selector-panel {
  padding: 12px;
  background: var(--bg-color);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 240px;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}

.theme-option {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  background: transparent;
  border: 1px solid transparent;
}

.theme-option:hover {
  background: var(--bg-color-secondary);
  border-color: var(--border-color);
}

.theme-option--active {
  background: var(--primary-color) !important;
  border-color: var(--primary-color) !important;
  color: white !important;
}

.theme-option--active .theme-label {
  color: white !important;
}

.theme-option--custom {
  grid-column: 1 / -1;
  justify-content: center;
  margin-top: 4px;
}

.theme-color-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  margin-right: 8px;
  border: 2px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.theme-label {
  font-size: 13px;
  color: var(--text-color);
  font-weight: 500;
}

.theme-divider {
  height: 1px;
  background: var(--border-color);
  margin: 8px 0;
}

.custom-icon {
  margin-right: 6px;
  font-size: 16px;
}

/* 自定义主题特殊样式 */
.theme-option--custom-theme {
  position: relative;
  display: flex !important;
  justify-content: space-between !important;
  align-items: center !important;
}

.theme-option-content {
  display: flex;
  align-items: center;
  flex: 1;
}

.theme-delete-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  color: var(--text-color-secondary);
  transition: all 0.2s ease;
  cursor: pointer;
  margin-left: 8px;
}

.theme-delete-btn:hover {
  background: #ff4d4f;
  color: white;
}

.theme-option--active .theme-delete-btn {
  color: rgba(255, 255, 255, 0.8);
}

.theme-option--active .theme-delete-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: white;
}
</style>